<?php

class PriceController extends AdminController {

	public $path = '';

	public function actionIndex() {
		$this->path=getcwd ().'/price/';

		$model = Price::model()->findByPk(1);

		if (isset($_POST['Price'])) {
			$model->attributes = $_POST['Price'];

			$file = CUploadedFile::getInstance($model, 'file');

			if ($model->validate()) {
				$this->cleanDir($this->path);

				$name = date('Y-m-d H:i:s') . '.' . $file->getExtensionName();

				$model->file = $name;
				$model->save();

				$file->saveAs($this->path . $name);
			}
		}

		$this->render('index', array('model' => $model));
	}

	public function cleanDir($dir) {
		$files = glob($dir . "/*");

		if (count($files) > 0) {
			foreach ($files as $file) {
				if (file_exists($file)) {
					unlink($file);
				}
			}
		}
	}

}
